package net.sf.rose.jdbc.bean;

import java.util.Map;

import net.sf.rose.jdbc.ConstantCode;
import net.sf.rose.jdbc.KeyGenerator;
import net.sf.rose.jdbc.bean.annotation.Column;
import net.sf.rose.jdbc.bean.annotation.Table;
import net.sf.rose.jdbc.bean.annotation.Type;

/**
 * 类说明：数据库表字段定义元数据类
 * 
 * @author 作者 fgg047 E-mail: 9110530@qq.com
 * @version 创建时间：2014-6-11
 */
@Table(name = "ADMIN_FIELD", description = "数据库字段定义说明")
public class FieldBean {

	@Column(name = "FIELD_ID", pk = true, type = Type.定长文本, description = "编号", canNull = false, size = 32, policy = "UUID")
	private String fieldID = null;

	@Column(name = "TABLE_ID", type = Type.定长文本, description = "编号", size = 32)
	private String tableID = null;

	/** 数据库字段名 */
	@Column(name = "FIELD_NAME", type = Type.变长文本, description = "字段名", size = 100)
	private String fieldName = null;

	/** 类的属性名 */
	@Column(name = "PROPERTY_NAME", type = Type.变长文本, description = "属性名", size = 100)
	private String propertyName = null;

	/** 数据类型 */
	@Column(name = "TYPE", type = Type.变长文本, description = "数据类型", size = 100)
	private String type = null;

	/** 数据库数据类型 */
	@Column(name = "DB_TYPE", type = Type.变长文本, description = "数据库数据类型", size = 100)
	private String dbType = null;

	/** Java类型 */
	@Column(name = "JAVA_TYPE", type = Type.变长文本, description = "Java类型", size = 100)
	private String javaType = null;

	/** 字段说明 */
	@Column(name = "DESCRIPTION", type = Type.变长文本, description = "字段说明", size = 250)
	private String description = null;

	/** 长度(整数位数) */
	@Column(name = "LEN", type = Type.变长文本, description = "长度", size = 100)
	private int size = 0;

	/** 小数位数 */
	@Column(name = "SCALE", type = Type.数字整型, description = "小数位数")
	private int scale = 0;

	/** 是否为主键，-1：否；1：是 */
	@Column(name = "PK", type = Type.数字整型, description = "是否为主键")
	private int pk = -1;

	/** 可否为NULL */
	/** 能否为空，-1：否；1：是 */
	@Column(name = "CAN_NULL", type = Type.数字整型, description = "可否为NULL")
	private int canNull = -1;

	/** 默认值 */
	@Column(name = "DEFAULT_VALUE", type = Type.变长文本, description = "默认值", size = 100)
	private String defaultValue = null;

	/** 是否需要持久化，-1：否；1：是 */
	@Column(name = "PRESIST", type = Type.数字整型, description = "是否需要持久化")
	private int presist = 1;

	/** 生成策略 */
	@Column(name = "POLICY", type = Type.变长文本, description = "生成策略", size = 100)
	private String policy = null;

	@Column(name = "SEQUENCE", type = Type.数字整型, description = "排序")
	private int sequence = 0;

	/** 生成器 */
	private transient KeyGenerator generator = null;

	public String getFieldName() {
		return fieldName;
	}

	public void setFieldName(String fieldName) {
		this.fieldName = fieldName;
	}

	public String getPropertyName() {
		return propertyName;
	}

	public void setPropertyName(String propertyName) {
		this.propertyName = propertyName;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public int getScale() {
		return scale;
	}

	public void setScale(int scale) {
		this.scale = scale;
	}

	public int getPk() {
		return pk;
	}

	public void setPk(int pk) {
		this.pk = pk;
	}

	public boolean isPk() {
		return pk == 1;
	}

	public int getCanNull() {
		return canNull;
	}

	public void setCanNull(int canNull) {
		this.canNull = canNull;
	}

	public boolean isCanNull() {
		return canNull == 1;
	}

	public String getDefaultValue() {
		return defaultValue;
	}

	public void setDefaultValue(String defaultValue) {
		this.defaultValue = defaultValue;
	}

	public boolean isPresist() {
		return presist == 1;
	}

	public int getPresist() {
		return presist;
	}

	public void setPresist(int presist) {
		this.presist = presist;
	}

	public String getJavaType() {
		return javaType;
	}

	public void setJavaType(String javaType) {
		this.javaType = javaType;
	}

	public String getDbType() {
		return dbType;
	}

	public void setDbType(String dbType) {
		if (dbType.indexOf(' ') != -1) {
			this.dbType = dbType.substring(0, dbType.indexOf(' '));
		} else {
			this.dbType = dbType;
		}
	}

	public String getPolicy() {
		return policy;
	}

	public void setPolicy(String policy) {
		this.policy = policy;
	}

	public KeyGenerator getBeanKeyGenerator() {
		return generator;
	}

	public void setBeanKeyGenerator(KeyGenerator generator) {
		this.generator = generator;
	}

	public Object uuid(Object bean) {
		if (generator == null)
			return null;
		synchronized (generator) {
			return generator.isNeedParamemters() ? generator.uuid(bean) : generator.uuid();
		}
	}

	public Object uuid(Map<String, Object> map) {
		if (generator == null)
			return null;
		synchronized (generator) {
			return generator.isNeedParamemters() ? generator.uuid(map) : generator.uuid();
		}
	}

	public void update(FieldBean bean) {
		this.canNull = bean.canNull;
		this.defaultValue = bean.defaultValue;
		this.fieldName = bean.fieldName;
		this.pk = bean.pk;
		this.size = bean.size;
		this.description = bean.description;
		this.type = bean.type;
	}

	public boolean equals(FieldBean bean) {
		if (!this.fieldName.equals(bean.fieldName))
			return false;
		if (!this.dbType.equalsIgnoreCase(bean.dbType)) {
			if (bean.type.equals(ConstantCode.DATA_TYPE_VAR_STRING)) {
				if (this.type.equals(ConstantCode.DATA_TYPE_INT_LIST)
						|| this.type.equals(ConstantCode.DATA_TYPE_STRING_LIST)
						|| this.type.equals(ConstantCode.DATA_TYPE_BIG_STRING)) {
				} else {
					return false;
				}
			} else {
				return false;
			}
		}
		return true;
	}

	public static boolean isDateType(FieldBean f) {
		if (f.javaType == null) {
			System.out.println(f.fieldName + "字段的Java类型为空！");
		}
		return f.javaType.equals(ConstantCode.JAVA_TYPE_DATE) || f.javaType.equals(ConstantCode.JAVA_TYPE_TIMESTAMP)
				? true
				: false;
	}

	public static boolean isStringType(FieldBean f) {
		return f.javaType.equals(ConstantCode.JAVA_TYPE_STRING) ? true : false;
	}

	public static boolean isNumberType(FieldBean f) {
		return f.javaType.equals(ConstantCode.JAVA_TYPE_INTEGER) || f.javaType.equals(ConstantCode.JAVA_TYPE_LONG)
				|| f.javaType.equals(ConstantCode.JAVA_TYPE_DOUBLE)
				|| f.javaType.equals(ConstantCode.JAVA_TYPE_BIGDECIMAL) ? true : false;
	}

	public static boolean isIntegerType(FieldBean f) {
		return f.javaType.equals(ConstantCode.JAVA_TYPE_INTEGER);
	}

	public static boolean isBoolean(FieldBean f) {
		return f.javaType.equals(ConstantCode.JAVA_TYPE_BOOLEAN);
	}

	public static boolean isLongType(FieldBean f) {
		return f.javaType.equals(ConstantCode.JAVA_TYPE_LONG);
	}

	public static boolean isArrayType(FieldBean f) {
		return f.type.equals(ConstantCode.DATA_TYPE_INT_LIST) || f.type.equals(ConstantCode.DATA_TYPE_STRING_LIST)
				? true
				: false;
	}

	public String getFieldID() {
		return fieldID;
	}

	public void setFieldID(String fieldID) {
		this.fieldID = fieldID;
	}

	public String getTableID() {
		return tableID;
	}

	public void setTableID(String tableID) {
		this.tableID = tableID;
	}

	public int getSequence() {
		return sequence;
	}

	public void setSequence(int sequence) {
		this.sequence = sequence;
	}

	public KeyGenerator getGenerator() {
		return generator;
	}

}
